import { baseApiUrl, path, poster } from "../../utils/config";
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({
  data: {
    sheet_show: false,
    poster: ""
  },
  onLoad(options) {
    this.setData({
      poster: app.globalData.poster
    })
  },
  changeSheet() {
    this.setData({ sheet_show: !this.data.sheet_show });
  },
  downloadFile() {
    let poster = this.data.poster;
    wx.downloadFile({
      url: poster,
      success: (res) => {
        let filePath = res.tempFilePath;
        wx.saveImageToPhotosAlbum({
          filePath,
          success: (res) => Toast.success('保存成功')
        })
      }
    })
  },
  onSelect() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.writePhotosAlbum']) {
          this.downloadFile();
          return false;
        }
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: () => this.downloadFile(),
            fail: (err) => {
              wx.showModal({
                title: '授权保存',
                content: '拒绝授权,将无法正常保存图片',
                confirmText: "授权",
                success: (res) => {
                  if (res.confirm) {
                    wx.openSetting({
                      success: (res) => Toast.success('授权成功')
                    })
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})